import { getUrlQuery } from '@/utils'
import * as Sentry from '@sentry/vue'

const { channelCode, memberCode } = getUrlQuery()

export function initSentry() {
  if (['uat1', 'prod'].includes(import.meta.env.VITE_ENV)) {
    try {
      Object.keys(sessionStorage).forEach(
        (key) => key.startsWith('sentry') && sessionStorage.removeItem(key),
      )

      Sentry.init({
        dsn: 'https://65ce3ec76afd200534851cd83a6079a7@sentry1.fuxiaoli.com/2',
        integrations: [
          new Sentry.BrowserTracing({
            // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
            tracePropagationTargets: ['localhost', /^https:\/\/yourserver\.io\/api/],
          }),
          new Sentry.Replay({
            maskAllText: false,
            blockAllMedia: false,
            networkDetailAllowUrls: [
              window.location.origin,
              /^https:\/\/(u1-|vip-gary)?(fls)?(1)?\.fuxiaoli\.com/,
            ],
            networkRequestHeaders: [
              'Cache-Control',
              'Referer',
              'Cookie',
              'Sk',
              'Sktoken',
              'User-Agent',
            ],
            networkResponseHeaders: ['Referrer-Policy'],
            networkCaptureBodies: true,
          }),
        ],
        // Performance Monitoring
        tracesSampleRate: 1.0, // 错误上报率-每次用户加载页面或在您的应用中导航时，都会发送一次请求  Capture 100% of the transactions
        // Session Replay
        replaysSessionSampleRate: 1.0, // 立即开始录制并持续整个用户会话的回放的采样率。 This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
        replaysOnErrorSampleRate: 1.0, // 发生错误时记录的回放的采样率。这种类型的重播将在错误发生之前记录长达一分钟的事件，并继续记录直到会话结束。 If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
        ignoreErrors: [
          "ReferenceError: Can't find variable: stopAudio",
          'Non-Error promise rejection captured with value: 非会员不能访问',
          'Non-Error promise rejection captured with value: 会员时间已过期，请重新购买会员',
          'Network Error',
        ],
      })

      Sentry.setUser({ username: memberCode })
      Sentry.setTag('channelCode', channelCode)
    } catch (e) {
      /* empty */
    }
  }
}
